<?php 
	require_once("User.php");
require_once("Battlefield.php");
	session_start();
	$db_connect = mysqli_connect("localhost", "root", "", "battleofthemages");

	if(isset($_SESSION['user'])){
		$user = unserialize($_SESSION['user']);
	}else{
		header("Location: index.php");
	}

    if(isset($_GET['id'])){
        $user = unserialize($_SESSION['user']);
        $username = $user->getUsername();
        $idSelectQuery = "SELECT username, user_id FROM user WHERE username=\"$username\"";
        $queryResult = mysqli_query($db_connect, $idSelectQuery);
        $row = mysqli_fetch_assoc($queryResult);
        $id = $row['user_id'];

        $battleId = $_GET['id'];
        $checkBattleQuery = "SELECT battle_id, user_one, user_two, status FROM battle WHERE battle_id=$battleId AND (user_one=$id OR user_two=$id) AND status=\"accepted\"";
        $checkResult = mysqli_query($db_connect, $checkBattleQuery);
        if(mysqli_num_rows($checkResult) < 1){
            header("Location: lobby.php");
        }
    }else{
        header("Location: lobby.php");
    }
?>
<!DOCTYPE html>
<html>
<head>
<title>Battle of the mages</title>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <meta charset="utf-8" />
        <link rel="stylesheet" type="text/css" href="style/style.css" />
        <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
		<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
		<script type="text/javascript" src="scripts/script.js"></script>
</head>
<body onload="onLoadLogin();">
	<header class="header">
	<img alt="Battle of the Mages Logo" src="images/botm_logo.png" id="logo" width="572px" height="334px"/>
	</header>
	<section id="main_section" style="background-image: url('images/botm_battlefield.png'); background-position: top center;">
	<img src="images/botm_border.png" alt="border" id="border" width="1437px" height="1573px"/>
        <div id="player"></div>
        <div id="player_deck"></div>
        <div id="opponent"></div>
        <div id="opponent_deck"></div>
        <div id="player_hand"></div>
        <div id="opponent_hand"></div>
        <div id="end_turn"><input type="image" src="images/botm_end_turn.png" id="end_turn_button"/><div id="end_turn_field"></div></div>
        <div id="player_field"></div>
        <div id="opponent_field"></div>
	</section>
    <footer>
        <ul id="footer_nav">
            <li><img src="images/botm_home.png"/></li>
            <li><img src="images/botm_howtoplay.png"/></li>
            <li><img src="images/botm_aboutus.png"/></li>
        </ul>
    </footer>
</body>
</html>

<?php
mysqli_close($db_connect);
?>